package com.xunfang.shareaddress.method;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.List;

import com.xunfang.shareaddress.util.GpsConfigFile;

/**
 * Socket通信类
 * 
 * @author lizhi
 * 
 */
public class Communicates {

	// 发送当前位置信息到服务器
	public void sendAddress(String username, String jingdu, String weidu) {
		try {
			Socket socket = new Socket(GpsConfigFile.ServerAddress,
					GpsConfigFile.Server_Port);
			PrintWriter pw = new PrintWriter(new BufferedWriter(
					new OutputStreamWriter(socket.getOutputStream())), true);
			pw.println("A");
			pw.println(username);
			pw.println(jingdu);
			pw.println(weidu);
			pw.println("byebye-->");

			pw.flush();
			pw.close();
			socket.close();

		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/*
	 * 检查用户名密码
	 */
	public boolean lCheck(String username, String password) {
		try {
			System.out.println("sendmessage----0--"
					+ GpsConfigFile.ServerAddress + "-----"
					+ GpsConfigFile.Server_Port);
			Socket cilent = new Socket(GpsConfigFile.ServerAddress,
					GpsConfigFile.Server_Port);
			System.out.println("sendmessage----1");
			if (cilent.getRemoteSocketAddress() != null) {
				System.out.println("连接上服务端！");
			} else {
				System.out.println("连接失败！");
			}
			PrintWriter pw = new PrintWriter(new BufferedWriter(
					new OutputStreamWriter(cilent.getOutputStream())), true);
			System.out.println("sendmessage----2");
			pw.println("U");
			pw.println(username);
			pw.println(password);
			pw.println("byebye-->");
			// }
			pw.flush();
			// Receivers r = new Receivers(cilent);
			// Receivers r=new Receivers();
			boolean flag = false;
			flag = getResultMessage(cilent);
			pw.close();
			cilent.close();
			System.out.println("标识符---1---" + flag);
			return flag;
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;

	}

	// 用户注册
	public boolean Register(String username, String password) {
		try {
			Socket cilent = new Socket(GpsConfigFile.ServerAddress,
					GpsConfigFile.Server_Port);
			PrintWriter pw = new PrintWriter(new BufferedWriter(
					new OutputStreamWriter(cilent.getOutputStream())), true);
			pw.println("Z");
			pw.println(username);
			pw.println(password);
			pw.println("byebye-->");
			pw.flush();
			boolean flag = false;
			flag = getResultMessage(cilent);
			pw.close();
			cilent.close();
			return flag;
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return false;
	}

	// 接收服务器返回数据
	public boolean getResultMessage(Socket cilent) {
		List list = new ArrayList<String>();
		try {
			String line = null;
			BufferedReader rbr = new BufferedReader(new InputStreamReader(
					cilent.getInputStream()));
			System.out.println(rbr);
			System.out.println("1-------------");
			// 读取数据
			while ((line = rbr.readLine()) != null) {
				if (!line.equals("byebye-->"))
					list.add(line);
				else
					break;
			}
			System.out.println("list--------------->" + list);
			// 关闭输入流
			rbr.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		System.out.println("2-------------");
		System.out.println(list.size());

		if (list.size() > 0) {
			if (list.get(0).toString().equals("OK")) {
				return true;
			}
			if (list.get(0).toString().equals("None")) {
				return false;
			}
		} else {
			return false;
		}
		return false;
	}

	// 向服务器发送数据请求
	public List<String> sendRequst() {
		try {
			Socket cilent = new Socket(GpsConfigFile.ServerAddress,
					GpsConfigFile.Server_Port);
			PrintWriter pw = new PrintWriter(new BufferedWriter(
					new OutputStreamWriter(cilent.getOutputStream())), true);
			pw.println("S");
			pw.println("byebye-->");
			pw.flush();
			List<String> list = new ArrayList<String>();
			list = getResultAddress(cilent);
			pw.close();
			cilent.close();
			return list;
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

	// 接收服务器返回的位置信息
	public List<String> getResultAddress(Socket cilent) {
		List<String> list = new ArrayList<String>();
		try {
			BufferedReader in = new BufferedReader(new InputStreamReader(
					cilent.getInputStream()));
			String line = null;
			while ((line = in.readLine()) != null) {
				if (!line.equals("byebye-->"))
					list.add(line);
				else
					break;
			}
			in.close();
			cilent.close();
			return list;
		} catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return null;
	}

}
